package com.fili.core;

import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.lwjgl.LWJGLException;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Nov 8, 2006
 * Time: 11:42:01 AM
 * Revision: 0
 */
public class Engine
{
    private float angle = 0;

    private long fpsFirstTime;
    private long fpsLastTime;
    private long fpsLastFrame;
    private long fpsCount;
    private float fps;

    public float getFps()
    {
        return fps;
    }

    public long getFpsLastTime()
    {
        return fpsLastTime;
    }

    public Engine()
    {
        fpsLastTime = System.currentTimeMillis();
        fpsFirstTime = System.currentTimeMillis();
        fpsLastFrame = 0;
        fpsCount = 0;
        fps = 0;
    }

    public DisplayMode[] getOpenGLModes()
    {
        try
        {
            return Display.getAvailableDisplayModes();
        }
        catch (LWJGLException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public boolean initOpenGL(DisplayMode mode)
    {
        return initOpenGL(mode, false);
    }

    public boolean initOpenGL(DisplayMode mode, boolean fullscreen)
    {
        return initOpenGL(mode, fullscreen, false);
    }

    public boolean initOpenGL(DisplayMode mode, boolean fullscreen, boolean vSync)
    {
        try
        {
            Display.setDisplayMode(mode);
            Display.setFullscreen(fullscreen);
            Display.setVSyncEnabled(vSync);
            Display.create();
            Display.setDisplayConfiguration(0,0,0);
        }
        catch (LWJGLException e)
        {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public void run()
    {
        while (!Display.isCloseRequested())
        {
            updateFps();
            Display.setTitle("FPS: " + fps);
            frame();
            Display.update();
/*            try
            {
                Thread.sleep(8);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }*/

            Display.sync(100);
        }
    }

    public void frame()
    {
        // clear the screen
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
        // center square according to screen size
        GL11.glPushMatrix();
        GL11.glTranslatef(Display.getDisplayMode().getWidth() / 2, Display.getDisplayMode().getHeight() / 2, 0.0f);

        // rotate square according to angle
        GL11.glRotatef(angle, 0, 0, 1.0f);
        angle += fpsLastFrame/10;

        GL11.glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
        // render the square
        GL11.glBegin(GL11.GL_QUADS);
        GL11.glTexCoord2f(0, 0);
        GL11.glVertex2i(-50, -50);
        GL11.glTexCoord2f(1, 0);
        GL11.glVertex2i(50, -50);
        GL11.glTexCoord2f(1, 1);
        GL11.glVertex2i(50, 50);
        GL11.glTexCoord2f(0, 1);
//        GL11.glColor4f(1, 1, 1, 1);
        GL11.glVertex2i(-50, 50);
        GL11.glEnd();

        GL11.glPopMatrix();
    }

    private void updateFps()
    {
        long t = System.currentTimeMillis();

        fpsCount++;

        if (t - fpsFirstTime >= 1000)
        {
            fps = (fpsCount * 1000.0f) / ((float) (t - fpsFirstTime));
            fpsFirstTime = t;
            fpsCount = 0;
        }

        fpsLastFrame = t - fpsLastTime;
        fpsLastTime = t;
    }
}
